<?php

class TraitOwner
{
	public $Traits = array( );
	
	// initializes the child traits
	public function InitTraits( )
	{
		foreach( $this->Traits as $Trait )
		{
			$Trait->TraitOwner = $this;
		}
	}
	
	// walks the traits and returns the first occurence of key
	public function __get( $Key )
	{
		$Count = count( $this->Traits );
		for( $i = $Count - 1; $i > -1; --$i )
		{
			$Trait = $this->Traits[$i];
			if(isset($Trait->$Key ))
			{
				return $Trait->$Key;
			}
		}
		
		throw new Exception('Parameter ' . $Key . ' does  not exist!');
	}
	
	// sets the value for the first trait it 
	// finds that already has the value set and return
	// if no other option set it for self
	public function __set( $Key, $Value )
	{
		$Count = count( $this->Traits );
		for( $i = $Count - 1; $i > -1; --$i )
		{
			$Trait = $this->Traits[$i];
			if(isset($Trait->$Key ))
			{
				$Trait->$Key = $Value;
				return;
			}			
		}

		$this->$Key = $Value;
	}
	
	// tries to call the method on all the traits
	public function __call( $Name, $Args )
	{
		$Count = count( $this->Traits );
		for( $i = $Count - 1; $i > -1; --$i )
		{
			$Trait = $this->Traits[$i];
			if(method_exists($Trait, $Name ))
			{
				$Code = '$Retval = $Trait->' . $Name . '( ' . implode( ',', $Args ) . ' );';
				eval( $Code );
				return $Retval;
			}			
		}
		
		throw new Exception('Method ' . $Name . ' does  not exist!');
	}
}

?>